package com.bjpowernode.service.impl;

import com.bjpowernode.entity.dto.ChartValueNameData;
import com.bjpowernode.entity.dto.ChartValueTimeData;
import com.bjpowernode.entity.dto.OverviewData;
import com.bjpowernode.mapper.*;
import com.bjpowernode.service.StatisticService;
import com.bjpowernode.utils.NumberUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * 统计信息实现类
 */
@Service
public class StatisticServiceImpl implements StatisticService {

    @Resource
    private TActivityMapper tActivityMapper;

    @Resource
    private TClueMapper tClueMapper;

    @Resource
    private TCustomerMapper tCustomerMapper;

    @Resource
    private TTranMapper tTranMapper;

    @Resource
    private TDicValueMapper tDicValueMapper;

    @Override
    public OverviewData getOverViewData() {
        // 查询各个数据
        /*private Integer goingActivityCount;  // 正在进行的市场活动
        private Integer totalActivityCount;  // 总的市场活动
        private Integer totalClueCount;  // 总的线索数
        private Integer totalCustomerCount;  // 总的客户数
        private BigDecimal successTranAmount; // 成功的交易额
        private BigDecimal totalTranAmount;   // 总的交易额*/
        // 查询正在进行的市场活动  和   总的市场活动
        Integer goingActivityCount = tActivityMapper.selectGoingActivityCount();
        Integer totalActivityCount = tActivityMapper.selectTotalActivityCount();
        // 查询 总的线索数
        Integer totalClueCount = tClueMapper.selectTotalClueCount();
        // 查询 总的客户数
        Integer totalCustomerCount = tCustomerMapper.selectTotalCustomerCount();
        // 查询 成功的交易额
        BigDecimal successTranAmount = tTranMapper.selectSuccessTranAmount();
        // 查询 总的交易额
        BigDecimal totalTranAmount = tTranMapper.selectTotalTranAmount();
        return OverviewData
                .builder()
                .goingActivityCount(goingActivityCount)
                .successTranAmount(successTranAmount)
                .totalActivityCount(totalActivityCount)
                .totalClueCount(totalClueCount)
                .totalCustomerCount(totalCustomerCount)
                .totalTranAmount(totalTranAmount)
                .build();
    }

    @Override
    public List<ChartValueNameData> loadFunnelData() {
        // 获取线索 | 客户 | 订单  | 成交 各总数
        // 查询 总的线索数
        Integer totalClueCount = tClueMapper.selectTotalClueCount();
        // 查询 总的客户数
        Integer totalCustomerCount = tCustomerMapper.selectTotalCustomerCount();
        // 查询 总的订单数
        Integer totalTranCount = tTranMapper.selectTotalTranCount();
        // 查询 总的成交数
        Integer totalSuccessTranCount = tTranMapper.selectTotalSuccessTranCount();
        // 以线索为100 , 求客户, 订单 , 成交 占线索的百分之几
        List<ChartValueNameData> chartValueNameDataList = new ArrayList<>();
        // 编辑工具类 , 传入值得结果
        chartValueNameDataList
                .add(ChartValueNameData
                        .builder()
                        .value(NumberUtil.compute(totalClueCount, totalClueCount))
                        .name("线索")
                        .build());
        chartValueNameDataList
                .add(ChartValueNameData
                        .builder()
                        .value(NumberUtil.compute(totalCustomerCount, totalClueCount))
                        .name("客户")
                        .build());
        chartValueNameDataList
                .add(ChartValueNameData
                        .builder()
                        .value(NumberUtil.compute(totalTranCount, totalClueCount))
                        .name("订单")
                        .build());
        chartValueNameDataList
                .add(ChartValueNameData
                        .builder()
                        .value(NumberUtil.compute(totalSuccessTranCount, totalClueCount))
                        .name("成交")
                        .build());
        return chartValueNameDataList;
    }

    @Override
    public List<ChartValueNameData> loadPieData() {
        // 线索来源名称 ---  线索来源对应的线索个数
        List<ChartValueNameData> chartValueNameDataList = tDicValueMapper.loadPieData();
        return chartValueNameDataList;
    }

    @Override
    public Integer[] loadBarData() {
        // 获取 当前月份之前月份 的 每个月的 市场活动个数
        // 月份   个数
        List<ChartValueTimeData> chartValueNameDataList = tActivityMapper.loadBarData();
        // 获取当前月份
        int monthValue = LocalDate.now().getMonthValue();
        // 定义数组
        Integer[] barDataArr = new Integer[monthValue];
        // 遍历集合
        chartValueNameDataList.forEach(item -> {
            // 获取该条数据的time  ,  判断该月份是否前面带0  , 并去0
            String time = item.getTime().startsWith("0") ? item.getTime().substring(1) : item.getTime();
            // 并将月份字符串转成数字
            int parseInt = Integer.parseInt(time);
            // 添加值
            barDataArr[parseInt - 1] = item.getValue();
        });
        return barDataArr;
    }
}
